// 用户信息
import { fetchUserInfo } from "@/api"

const logo = 'https://image.chushouya.com/uploads/file/20231121/20231121085744_145.png'

const state = {
  userInfo: { userId: 0 }, // 用户信息

  channel: uni.getStorageSync('channel') || undefined,

  parentId: '',

  shipping: {
  }, // 收货地址

  openId:""
}

const mutations = {
  // 更新用户信息
  UPDATE_USERINFO: (state, info) => {
    state.userInfo = info
  },

  // 退出登录
  UPDATE_LOGOUT: (state) => {
    state.userInfo = { userId: 0 }
  },

  UPDATE_PARENTID: (state, parentId) => {
    state.parentId = { parentId }
  },

  UPDATE_CHANNEL: (state, channel) => {
    state.channel = { channel }
  },
  //更新收货地址
  UPDATE_SHIPPING: (state, shipping) => {
    console.log('更新收货地址', shipping)
    state.shipping = shipping 
  },
  // 更新用户openId
  UPDATE_OPENID: (state, openId) => {
    state.openId = openId
  }
}

const actions = {
  // 登录
  userLogin({ commit }, data) {
    uni.setStorageSync('token', data.token)
    getApp().globalData.token = data.token
    commit('UPDATE_USERINFO', data)
  },

  // 更新用户信息
  updateUserInfo({ commit }, info) {
    commit('UPDATE_USERINFO', info)
  },

  // 更新收货地址
  updateShipping({ commit }, shipping) {
    commit('UPDATE_SHIPPING', shipping)
  },
  // 更新用户openId
  updateOpenId({ commit }, openId) {
    commit('UPDATE_OPENID', openId)
  },
  // 获取用户信息
  loadUserInfo({ commit }) {
    return new Promise(resolve => {
      fetchUserInfo().then(({ data, code }) => {
        if (code === 200) {
          if (!data.avatar) {
            data.avatar = logo
          }
          commit('UPDATE_USERINFO', data)
          resolve(data)
        } else {
          resolve(false)
        }
      })
    })
  },

  // 退出登录
  // loadLogout({ commit }) {
  //   return new Promise(resolve => {
  //     getUserLogout().then(({ data, code }) => {
  //       if (code === 200) {
  //         getApp().globalData.token = ''
  //         uni.setStorage({ key: 'token', data: '' })
  //         commit('UPDATE_LOGOUT')
  //         resolve(data)
  //       } else {
  //         resolve(false)
  //       }
  //     })
  //   })
  // },

  // 退出登录
  loadLogout({ commit }) {
    console.log('退出登录')
    return new Promise(resolve => {
      uni.showLoading({
        title: '正在退出登录',
        mask: true
      })
      getApp().globalData.token = ''
      uni.setStorage({ key: 'token', data: '' })
      commit('UPDATE_LOGOUT')
      uni.hideLoading()
      resolve(true)
    })
  },

  // 退出登录
  onLogout({ commit }) {
    getApp().globalData.token = ''
    uni.setStorage({ key: 'token', data: '' })
    commit('UPDATE_LOGOUT')
  },

  // 更新 parentId
  updateParentId({ commit }, parentId) {
    uni.setStorage({ key: 'parentId', data: parentId })
    commit('UPDATE_PARENTID', parentId)
  },

  // 更新 referer
  updateChannel({ commit }, referer) {
    uni.setStorageSync('channel', referer)
    // 判断是否有来源referer, 不存在就添加来源渠道
    // const fromreferer = uni.getStorageSync('fromreferer')

    // if (!fromreferer) {
      // console.log('添加来源===>fromreferer', referer)
      // uni.setStorageSync('channel', referer)
    // } else if (fromreferer !== referer) {
      // console.log('添加来源===>referer', referer)
      // uni.setStorageSync('referer', referer)
    // }

    commit('UPDATE_CHANNEL', referer)
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions,
}